// 坦克类，可以创建玩家与非玩家，区分阵营
class CreateTank {
  constructor(
    canvas,
    ctx,
    camp, // 以数字区分
    speed,
    color,
    create_pointer = [0, 0],
    id
  ) {
    this.id = id;
    this.cvs_width = canvas.width;
    this.cvs_height = canvas.height;
    this.ctx = ctx;
    // 阵营
    this.camp = camp;
    this.tank_pointer = create_pointer;
    this.speed = speed;
    this.tank_color = color;
    this.gun = null
    this.tank_level = 1
    this.tank_width = 40
    // 坦克是几边行
    this.point_num = 4
    this.target_pointer = [0, 0]
    this.tank_polygon_point = []
    this.tank_polygon_point_num = [[-20, -20], [20, -20], [20, 20], [-20, 20]]
    // 方向
    this.orientation = {
      top: false,
      left: false,
      right: false,
      bottom: false
    }
    // 火力
    this.bullet_level = 1
    // 子弹id, 每次开火自增1，达到100后，归0
    this.bullet_id_num = 0
    // 在屏幕中存在的子弹数组
    this.bullet_num = 0
    // 初始化绘制
    this.computeDrawPoint()
    this.drawTankCar()
    this.drawTankGun()
  }
  // 计算坦克的绘制位置
  computeDrawPoint() {
    for (let i = 0; i < this.point_num; i++) {
      this.tank_polygon_point[i] = [
        this.tank_polygon_point_num[i][0] + this.tank_pointer[0],
        this.tank_polygon_point_num[i][1] + this.tank_pointer[1]
      ]
    }
  }
  // 初始化绘制坦克车身
  drawTankCar() {
    this.ctx.fillStyle = this.tank_color;
    this.ctx.beginPath();
    this.ctx.moveTo(this.tank_polygon_point[0][0], this.tank_polygon_point[0][1])
    this.ctx.lineTo(this.tank_polygon_point[1][0], this.tank_polygon_point[1][1])
    this.ctx.lineTo(this.tank_polygon_point[2][0], this.tank_polygon_point[2][1])
    this.ctx.lineTo(this.tank_polygon_point[3][0], this.tank_polygon_point[3][1])
    this.ctx.closePath();
    this.ctx.fill();
  }
  // 初始化绘制坦克炮管
  drawTankGun() {
    const init_x = this.tank_pointer[0];
    const init_y = this.tank_pointer[1];
    const target_x = this.target_pointer[0];
    const target_y = this.target_pointer[1];
    this.ctx.fillStyle = "#f00";
    this.ctx.save();
    this.ctx.translate(init_x, init_y);
    this.ctx.rotate(
      Number(Math.atan2(target_y - init_y, target_x - init_x).toFixed(2))
    );
    this.ctx.beginPath();
    this.ctx.moveTo(0, -2);
    this.ctx.lineTo(40, -2);
    this.ctx.lineTo(40, 2);
    this.ctx.lineTo(0, 2);
    this.ctx.closePath();
    this.ctx.restore();
    this.ctx.fill();
  }
  // 坦克移动
  tankMove() {
    const x = this.tank_pointer[0];
    const y = this.tank_pointer[1];
    var tempX = x;
    var tempY = y;
    if (this.orientation.top && y > 0) {
        this.tank_pointer[1] -= this.speed;
      // tempY -= this.speed;
    }
    if (this.orientation.left && x > 0) {
        this.tank_pointer[0] -= this.speed;
      // tempX -= this.speed;
    }
    if (this.orientation.bottom && y < this.cvs_height) {
        this.tank_pointer[1] += this.speed;
      // tempY += this.speed;
    }
    if (this.orientation.right && x < this.cvs_width) {
        this.tank_pointer[0] += this.speed;
      // tempX += this.speed;
    }
    this.computeDrawPoint()
    // if (websocket.readyState == WebSocket.OPEN) {
    //   websocket.send(
    //     JSON.stringify({
    //       type: "move",
    //       id: this.id,
    //       x: tempX,
    //       y: tempY,
    //     })
    //   );
    // }
  }
  // 坦克开火
  fire() {
    if (this.bullet_num >= this.bullet_level) {
      return false
    }
    const init_x = this.tank_pointer[0]
    const init_y = this.tank_pointer[1]
    const target_x = this.target_pointer[0]
    const target_y = this.target_pointer[1]
    const tank_gun_rotate = Math.atan2(target_y - init_y, target_x - init_x)
    const tank_gun_x = Number(Math.cos(tank_gun_rotate).toFixed(2)) * 40
    const tank_gun_y = Number(Math.sin(tank_gun_rotate).toFixed(2)) * 40
    const bullet_id = this.setBulletId()
    const bullet = new TankDefaultBullet(
        bullet_id,
        this.id,
        this.bullet_level,
        this.ctx,
        this.camp,
        [init_x + tank_gun_x, init_y + tank_gun_y],
        [target_x, target_y]
    )
    this.bullet_num++
    return bullet
  }
  // 绘制整个坦克
  drawTankFull() {
    this.tankMove()
    this.drawTankCar()
    this.drawTankGun()
  }

  // 坦克炮管方向更新
  updateTankGunRotate(point = [0, 0]) {
    this.target_pointer = point;
  }

  // 设置子弹id
  setBulletId() {
    if (++this.bullet_id_num > 100) {
      this.bullet_id_num = 0
    }
    return this.id + '_' + this.bullet_id_num
  }

  // 子弹消失或发生碰撞，子弹数量减1
  deleteBullet() {
    this.bullet_num--
  }

  // 子弹碰撞到坦克，清除坦克自身
  deleteTankSelf() {
    this.deleteAllTank()
  }
  // 删除在坦克实例和全局子弹列表中的子弹对象
  deleteAllTank() {
    this.bulletHideAfterSpecialEffects()
    all_tank.delete(this.id)
    computer_players_tank.delete(this.id)
  }
  // 子弹在消失后执行特效, 可以继承
  bulletHideAfterSpecialEffects() {
    const effect = new NormalSpecialEffect(this.id, this.bullet_level, this.tank_pointer, 40, this.ctx)
    special_effects_list.set(this.id, effect)
    this.setCrashAfterOperation()
  }

  // 设置爆炸后的执行函数
  setCrashAfterOperation() {
    if (this.camp !== 1) {
      createComputerTank()
    }
  }
}
